#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // disegno su canvas
    m_scene = new QGraphicsScene();
    ui->graphicsView->setScene(m_scene);
    int dim = 80;

    // crea i GraphicsItem
    m_p1 = new Pump(dim, 1);
    m_p1->setPos(100,100);
    m_scene->addItem(m_p1);

    m_p2 = new Pump(dim, 0);
    m_p2->setPos(400, 200);
    m_scene->addItem(m_p2);

    // aggiorna la GraphicsScene
    m_scene->invalidate();
    update();

    QTimer::singleShot(2000, this, SLOT(pump1StatusChanged()));
    QTimer::singleShot(2500, this, SLOT(pump2StatusChanged()));
    QTimer::singleShot(2900, this, SLOT(pump2StatusChanged()));
    QTimer::singleShot(3500, this, SLOT(pump2StatusChanged()));
    QTimer::singleShot(5000, this, SLOT(pump1StatusChanged()));

    //        QRectF rect;
    //        rect.adjust(300,60,20,10);

    //        QPen penna;
    //        penna.setColor("red");
    //        penna.setWidth(3);

    //        QBrush pennello;
    //        pennello.setStyle(Qt::SolidPattern);
    //        pennello.setColor(Qt::red);


    //        scene->addEllipse(rect, penna, pennello);
    //        ui->graphicsView->setScene(scene);
    //        QList<QGraphicsItem *> list = (*scene).items();
    //        QGraphicsItem* item = list.at(0);


    // caricamento immagine statica in una label
    QString file(":/images/images/Yellow_Dot_by_DraGoth.png");
    QPixmap img;
    if(img.load(file, "png"))
        ui->immagine->setPixmap(img);
    ui->immagine->hide();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_chillerButton_clicked()
{
    ui->stackedWidget->setCurrentIndex(2);
}

void MainWindow::on_processButton_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
}

void MainWindow::on_synopticButton_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
}

void MainWindow::on_historyButton_clicked()
{
    ui->stackedWidget->setCurrentIndex(3);
}

void MainWindow::on_settingsButton_clicked()
{
    ui->provaInserimento->setBaseSize(300, 200);
    ui->stackedWidget->setCurrentIndex(4);
    //    QString s(&(ini.getAnalogInput("AI01").getDescription().at(0)));
    QString s(&(ini.toString().at(0)));
    ui->provaInserimento->setText(s);
}

void MainWindow::pump1StatusChanged(){
    m_p1->changeStatus();

    m_scene->invalidate();
    update();
}

void MainWindow::pump2StatusChanged(){
    m_p2->changeStatus();

    m_scene->invalidate();
    update();
}
